#include "esp_camera.h"
#include <WiFi.h>
#include "esp_http_client.h"
#include <ArduinoJson.h>

const int brightLED = 4;
static String httpResponseString;
const int ledFreq = 50;       // PWM settings
const int ledChannel = 15;    // camera uses timer1
const int ledRresolution = 8; // resolution (8 = from 0 to 255)
bool flashRequired = {{&flashLed}};

void brightLed(byte ledBrightness) {
  ledcWrite(ledChannel, ledBrightness); // change LED brightness (0 - 255)
}

void setupFlashPWM() {
  ledcSetup(ledChannel, ledFreq, ledRresolution);
  ledcAttachPin(brightLED, ledChannel);
  brightLed(0);
}

#define CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27

#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22

esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
  if (evt->event_id == HTTP_EVENT_ON_DATA) {
    httpResponseString.concat((char *)evt->data);
  }
  return ESP_OK;
}

static esp_err_t take_send_photo() {
  if (!WiFi.isConnected()) {
    WiFi.begin();
  }

  if (flashRequired) {
    brightLed(255);
    delay(300);
  }

  Serial.println("Taking picture...");
  camera_fb_t *fb = NULL;
  esp_err_t res = ESP_OK;
  fb = esp_camera_fb_get();
  if (flashRequired) {
    brightLed(0);
  }
  if (!fb) {
    Serial.println("Camera capture failed");
    return ESP_FAIL;
  }
  httpResponseString.clear();
  esp_http_client_handle_t http_client;
  esp_http_client_config_t config_client = {0};
  config_client.url = "http://images.bemfa.com/upload/v1/upimages.php";
  config_client.event_handler = _http_event_handler;
  config_client.method = HTTP_METHOD_POST;
  http_client = esp_http_client_init(&config_client);
  esp_http_client_set_post_field(http_client, (const char *)fb->buf, fb->len); // 设置http发送的内容和长度
  esp_http_client_set_header(http_client, "Content-Type", "image/jpg");        // 设置http头部字段
  esp_http_client_set_header(http_client, "Authorization", {{&uid}});          // 设置http头部字段
  esp_http_client_set_header(http_client, "Authtopic", {{&topicId}});          // 设置http头部字段
  esp_http_client_set_header(http_client, "wechatmsg", {{&wechatMsg}});        // 设置http头部字段
  esp_http_client_set_header(http_client, "wecommsg", {{&wechatMsgCam}});      // 设置http头部字段
  esp_http_client_set_header(http_client, "picpath", {{&wechatCamPath}});      // 设置http头部字段
  esp_err_t err = esp_http_client_perform(http_client);                        // 发送http请求
  if (err == ESP_OK) {
    // json数据解析
    StaticJsonDocument<200> doc;
    DeserializationError error = deserializeJson(doc, httpResponseString);
    if (error) {
      Serial.print(F("deserializeJson() failed: "));
      Serial.println(error.c_str());
    }
    String url = doc["url"]; 
    {{&funcBranch}}
  }
  esp_http_client_cleanup(http_client);
  esp_camera_fb_return(fb);
  return res;
}